﻿/**
 * Created by Administrator on 2017/9/5.
 *公客转公客
 */
define(['api'],function (api) {
    var ShiftCustomerToOtherDept = {
        name: 'shift-customer-to-other-dept',
        template: ''
        + '<Modal title="转移客户"'
        + '       v-model="showShiftCustomerToOtherDept"'
        + '       width="360"'
        + '       ok-text="确认"'
        + '       v-on:on-cancel="closeShift"'
        + '       v-on:on-ok="shiftCustomerToDept"'
        + '       v-bind:closable="false"'
        + '       v-bind:mask-closable="false"'
        + '       class-name="turnClientWrap  vertical-center-modal shift-customer-to-other-staff">'
        + '<div class="modal-content">'
        + ' <ul class="pri-ul">'
        + ' <li class="pri-li">'
        + '     <span class="model-left removeLeft">选择公司部门</span>'
        + '     <p class="model-right remove-right">'
        + '         <Cascader v-bind:data="nodeOption" v-model="selectedNode"  filterable  v-bind:clearable="false" change-on-select  v-on:on-change="selectNodeId"></Cascader>'
        //+ '         <i-select v-model="shiftInfo.newNodeId" v-on:on-change="selectNodeId">'
        //+ '             <i-option v-for="item in nodeOption" :value="item.Id" :key="item.NameText">'
        //+ '                 {{item.NameText}}'
        //+ ' 			</i-option>'
        //+ ' 		</i-select>'
        + ' 	</p>'
        + ' </li >'
        + '    <li class="pri-li" v-show="shiftInfo.isDept">'
        + '         <span class="model-left removeLeft">清空回访记录</span>'
        + '         <p class="model-right remove-right">'
        + '             <Radio-group v-model="shiftInfo.isClearVisit">'
        + '                 <Radio label="true"><span>是</span></Radio>'
        + '                 <Radio label="false"><span>否</span></Radio>'
        + '             </Radio-group>'

        + '         </p>'
        + '     </li>'
        + '     <li class="pri-li">'
        + '         <span class="model-left removeLeft">转移该部门全部客户</span>'
        + '         <p class="model-right remove-right">'
        + '             <Radio-group v-model="shiftInfo.isShiftAll">'
        + '                 <Radio label="true"><span>是</span></Radio>'
        + '                 <Radio label="false"><span>否</span></Radio>'
        + '             </Radio-group>'
        + '         </p>'
        + '     </li>'
        + '  </ul>'
        + '</div >'
        + '</Modal>',
        props: {
            poolType: String
        },
        data: function () {
            return {
                nodeOption: [],
                shiftInfo: {
                    newNodeId: "",
                    oldNodeId: "",
                    isClearVisit: "false",
                    isShiftAll: "false",
                    customerList: [],
                    isDept:true,
                },
                showShiftCustomerToOtherDept: false,
                nodeDeptOption: [],
                nodeCompanyOption: [],
                selectedNode: [],
            }
        },
        methods: {
            closeShift: function () {
            },
            shiftCustomerToDept() {
                var thisShift = this;
                if (thisShift.shiftInfo.newNodeId === ""){
                    thisShift.$Message.error('没有选择目标客户池');
                    return;
                }
                if (thisShift.shiftInfo.newNodeId === thisShift.shiftInfo.oldNodeId) {
                    thisShift.$Message.error('不能转给相同的客户池');
                    return;
                }
                api.Post_Shift_Customer_ToOtherDept(thisShift.shiftInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisShift.$Message.success('转移成功');
                            //触发shiftCustomerToOtherDeptSuccess
                            window.bus.$emit("shiftCustomerToOtherDeptSuccess");
                        } else {
                            thisShift.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisShift.$Message.success('转移失败');
                    });
            },
            selectNodeId: function (value) {
                //var v = value[value.length - 1];
                //for (var i = 0; i < this.nodeOption.length; i++) {
                //    if (v === this.nodeOption[i].Id) {
                //        this.shiftInfo.isDept = this.nodeOption[i].NodeType==1;
                //        break;
                //    }
                //}
            },
        },
        mounted: function () {
            var thisShift = this;
            //监听showShiftCustomerToOtherDept
            window.bus.$on("showShiftCustomerToOtherDept", function (data) {
                thisShift.shiftInfo.customerList = data.customerList;
                thisShift.shiftInfo.oldNodeId = data.oldNodeId;
                thisShift.nodeOption = [];
                if (thisShift.poolType === "DeptPool"){
                    thisShift.nodeOption = thisShift.nodeDeptOption;
                }
                if (thisShift.poolType === "CompanyPool") {
                    thisShift.nodeOption = thisShift.nodeCompanyOption;
                }
                this.$nextTick(function () {
                    thisShift.showShiftCustomerToOtherDept = true;
                });
                //for (var i = 0; i < thisShift.nodeCompanyOption.length; i++) {
                //    if (data.oldNodeId != thisShift.nodeCompanyOption[i].value) {
                //        thisShift.nodeOption.push(thisShift.nodeCompanyOption[i]);
                //    }
                //}
                //for (var i = 0; i < thisShift.nodeDeptOption.length; i++) {
                //    if (data.oldNodeId != thisShift.nodeDeptOption[i].value) {
                //        thisShift.nodeOption.push(thisShift.nodeDeptOption[i]);
                //    }
                //}
                //触发deleteFocus
                window.bus.$emit("deleteFocus");
            });

            //$.get("/Department/GetDepartmentByCompanyId", { companyId: localStorage.companyId },function (data) {
            //    if (data.IsSuccess) {
            //        thisShift.nodeDeptOption = data.ResultObject;
            //    } else {
            //        thisShift.$Message.error(data.ErrMsg);
            //    }
            //});
            //$.get("/Company/GetCompanyById", { companyId: localStorage.companyId }, function (data) {
            //    if (data.IsSuccess) {
            //        thisShift.nodeCompanyOption.push(data.ResultObject);
            //    } else {
            //        thisShift.$Message.error(data.ErrMsg);
            //    }
            //});

            api.GetOrganizaitonNode({ "nodeId": localStorage.companyId })
                .done(function (res) {
                    if (res.IsSuccess === true) {
                        thisShift.nodeDeptOption = [res.ResultObject];
                        thisShift.nodeCompanyOption = [res.ResultObject];
                    } else {
                        thisShift.$Message.error(res.ErrMsg);
                    }
                })
                .fail(function () {
                    thisShift.$Message.error('查询出错');
                });
        },
        watch: {
            selectedNode: function (newNode) {
                newNode.length === 0 ? this.shiftInfo.newNodeId = "" : this.shiftInfo.newNodeId = newNode[newNode.length - 1];
            },
        },
    };
    return ShiftCustomerToOtherDept;
});
